//
//  CYDiscoverCullCell.m
//  cyjf
//
//  Created by tauCross on 2017/11/27.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYDiscoverCullCell.h"

#import <UIButton+WebCache.h>



@implementation CYDiscoverCullCellObject

@end



@interface CYDiscoverCullCell ()

@property(nonatomic, strong)UILabel *titleLabel;
@property(nonatomic, strong)UIButton *cull1;
@property(nonatomic, strong)UIButton *cull2;

@property(nonatomic, strong)CYDiscoverCullCellObject *object;

@end


@implementation CYDiscoverCullCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self)
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [UIColor whiteColor];
        
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.text = @"活动精选";
        self.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightRegular];
        self.titleLabel.textColor = HEXCOLOR(@"444444");
        [self.titleLabel sizeToFit];
        [self.contentView addSubview:self.titleLabel];
        
        self.cull1 = [[UIButton alloc] init];
        self.cull1.layer.masksToBounds = YES;
        self.cull1.layer.cornerRadius = 5;
        [self.cull1 setBackgroundImage:CYImage(@"find_top_pic_empty") forState:UIControlStateNormal];
        [self.cull1 addTarget:self action:@selector(onCull1TouchUpInside) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.cull1];
        
        self.cull2 = [[UIButton alloc] init];
        self.cull2.layer.masksToBounds = YES;
        self.cull2.layer.cornerRadius = 5;
        [self.cull2 setBackgroundImage:CYImage(@"find_top_pic_empty") forState:UIControlStateNormal];
        [self.cull2 addTarget:self action:@selector(onCull2TouchUpInside) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.cull2];
    }
    return self;
}

+ (CGFloat)heightForObject:(id)object atIndexPath:(NSIndexPath *)indexPath tableView:(UITableView *)tableView
{
    return 66 + (SCREEN_WIDTH - 55) * 70 / 320;
}

- (BOOL)shouldUpdateCellWithObject:(id)object
{
    self.object = object;
    
    if(self.object.models.count == 2)
    {
        if([self.object.models.firstObject isKindOfClass:[CYDataAPIAdvertisementModel class]])
        {
            [self.cull1 sd_setBackgroundImageWithURL:self.object.models.firstObject.image.tc_url forState:UIControlStateNormal placeholderImage:CYImage(@"find_top_pic_empty")];
        }
        if([self.object.models.lastObject isKindOfClass:[CYDataAPIAdvertisementModel class]])
        {
            [self.cull2 sd_setBackgroundImageWithURL:self.object.models.lastObject.image.tc_url forState:UIControlStateNormal placeholderImage:CYImage(@"find_top_pic_empty")];
        }
    }
    else
    {
        [self.cull1 setBackgroundImage:CYImage(@"find_top_pic_empty") forState:UIControlStateNormal];
        [self.cull2 setBackgroundImage:CYImage(@"find_top_pic_empty") forState:UIControlStateNormal];
    }
    
    [self setNeedsLayout];
    return YES;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.titleLabel.left = 20;
    self.titleLabel.centerY = 23;
    
    self.cull1.height = (SCREEN_WIDTH - 55) * 70 / 320;
    self.cull1.width = self.cull1.height * 160 / 70;
    self.cull2.size = self.cull1.size;
    
    self.cull1.left = 20;
    self.cull1.bottom = self.contentView.height - 20;
    self.cull2.right = self.contentView.width - 20;
    self.cull2.bottom = self.cull1.bottom;
}

#pragma mark - Control Events
- (void)onCull1TouchUpInside
{
    if([self.object.models.firstObject isKindOfClass:[CYDataAPIAdvertisementModel class]] && self.object.onClickModel)
    {
        self.object.onClickModel(self.object.models.firstObject);
    }
}

- (void)onCull2TouchUpInside
{
    if(self.object.models.count == 2 && [self.object.models.lastObject isKindOfClass:[CYDataAPIAdvertisementModel class]] && self.object.onClickModel)
    {
        self.object.onClickModel(self.object.models.lastObject);
    }
}

@end
